package cn.jiyun.service.impl;

import cn.jiyun.dao.SetmealDao;
import cn.jiyun.entity.PageResult;
import cn.jiyun.entity.QueryPageBean;
import cn.jiyun.pojo.CheckGroup;
import cn.jiyun.pojo.CheckItem;
import cn.jiyun.pojo.Setmeal;
import cn.jiyun.serivce.SetmealService;
import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;

@Service
public class SetmealServiceImpl implements SetmealService {

    @Autowired
    private SetmealDao setmealDao;

    @Override
    public PageResult findPage(QueryPageBean queryPageBean) {
        PageHelper.startPage(queryPageBean.getCurrentPage(),queryPageBean.getPageSize());
        Page<Setmeal> page = setmealDao.findList(queryPageBean.getQueryString());
        return new PageResult(page.getTotal(),page.getResult());
    }

    @Override
    public void add(Setmeal setmeal, Integer[] checkgroupIds) {
//        1、添加基本信息
        setmealDao.add(setmeal);
        // 2、添加中间表
        for (Integer checkgroupId : checkgroupIds) {
            setmealDao.addSG(checkgroupId,setmeal.getId());
        }
    }

    @Override
    public List<Integer> findCheckItemIdsByGid(Integer id) {
        return setmealDao.findCheckItemIdsByGid(id);
    }

    @Override
    public void edit(Setmeal setmeal, Integer[] checkgroupIds) {
        //1、修改基本信息
        setmealDao.edit(setmeal);
        //2、删除中间表
        setmealDao.delGIByGid(setmeal.getId());
        //3、添加中间表
        for (Integer checkgroupId : checkgroupIds) {
            setmealDao.addSG(checkgroupId,setmeal.getId());
        }
    }

    @Override
    public List<Setmeal> getSetmeal() {
        return setmealDao.findAll();
    }

    @Override
    public Setmeal findById(Integer id) {
        Setmeal setmeal = setmealDao.findById(id);//套餐基本信息

        //根据套餐id查询检查组信息
        List<CheckGroup> checkGroupList = setmealDao.findCheckGroupsBySid(id);
        setmeal.setCheckGroups(checkGroupList);

        for (CheckGroup checkGroup : checkGroupList) {
            //根据检查组id查询检查项信息
            List<CheckItem> checkItemList = setmealDao.findItemsByGid(checkGroup.getId());
            checkGroup.setCheckItems(checkItemList);
        }
        return setmeal;
    }

}
